#!/usr/bin/python

##
#
# Snaarpy: Guitar chord parser and formatter
# Copyright (C) 2009 Alberto Gonzalez
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
##

from optparse import OptionParser
import getopt
import sys
import os.path

import snaarpy
import logging

def print_usage():
    pass

if __name__ == '__main__':
    parser = OptionParser("%prog [options] input-files...", version="%prog 0.1-dev")
    
    parser.add_option("-l", "--list-plugins", dest="list_plugins", action="store_true",
                      help="list available input/output plugins")
    parser.add_option("-f", "--output-format", dest="output_format", default="null",
                      metavar="FORMAT", help="use FORMAT for output formatting")
    parser.add_option("-o", "--output", dest="output_file", metavar="FILE",
                      help="place output in FILE")
    parser.add_option("-s", "--sort", dest="sort_attribute", default="artist",
                      metavar="ATTR", help="sort list by ATTR")
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
                      help="produce verbose output")
    
    (options, args) = parser.parse_args()
    
    if options.list_plugins:
        snaarpy.input.print_plugin_list()
        snaarpy.output.print_plugin_list()
        sys.exit(0)
    
    if options.verbose:
        level = logging.DEBUG
    else:
        level = logging.INFO
    
    logging.basicConfig(format = '%(levelname)-7s: %(message)s', level = level)
    logger = logging.root
            

    if len(args) == 0:
        parser.print_help()
        sys.exit(-1)
    
    # Guess from extensions without dots
    formats = [ os.path.splitext(arg)[1][1:] for arg in args ] 
    
    try:
        snaarpy.process(args, formats, 
                        output_file=options.output_file, 
                        output_format=options.output_format, 
                        sort_by=options.sort_attribute)
    except snaarpy.input.UnsupportedInputError, ex:
        logger.error(ex)
    except snaarpy.output.UnsupportedOutputError, ex:
        logger.error(ex)
    except IOError, err:
        logger.error(err)
        
    